﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace Login.ADL
{
    public class Helpsql
    {
        /// <summary>
        /// 数据的访问
        /// </summary>

        private static readonly string conter = ConfigurationManager.ConnectionStrings["consting"].ConnectionString;
        //链接服务器语句;

        //表格的提取
        #region
        public static DataSet getdata(string sql)
        {
            using (SqlConnection conn = new SqlConnection(conter))
            {
                conn.Open();
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                return ds;
            }
        }
        #endregion

        //表中信息管理
        #region
        public static void getxiu(string sql)
        {
            using (SqlConnection conn = new SqlConnection(conter))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql,conn);
                cmd.ExecuteNonQuery();
            }
        }
        #endregion

        //表中相关数目返回
        #region
        public static int getshu(string sql)
        {
            using (SqlConnection conn = new SqlConnection(conter))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                int n = Convert.ToInt32(cmd.ExecuteScalar());
                return n;
            }
        }
        #endregion

        //学生总实验提取
        #region
        public static string[] getzu(string sql,int n)
        {
            using (SqlConnection conn = new SqlConnection(conter))
            {
                int i = 0;
                string[] str = new string[n];
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {
                    str[i] = sdr[2].ToString();
                    i = i + 1;
                }
                sdr.Close();
                return str;
            }
        }
        //得到对应班级所上实验的ID
        public static int[] getZuOne(string sql, int n)
        {
            using (SqlConnection conn = new SqlConnection(conter))
            {
                int i = 0;
                int[] str = new int[n];
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {
                    str[i] =Convert.ToInt32(sdr[2].ToString());
                    i = i + 1;
                }
                sdr.Close();
                return str;
            }
        }
        #endregion

        //指定空的提取
        #region
        public static string getgeti(string sql)
        {
            using (SqlConnection conn = new SqlConnection(conter))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandTimeout = 0;
                string geti = Convert.ToString(cmd.ExecuteScalar());
                return geti;
            }
        }
        #endregion


        //通过序数找到相关实验
        #region
        public static string xueShuZhaoShiYan(int n)
        {
                string sql = "select ExperimentNo from tb_Experiment where ExperimentId='{0}'";
                sql = string.Format(sql,n);
                string shiyan = getgeti(sql);
                return shiyan;
        }
        #endregion


    }
}
